﻿using Microsoft.Xaml.Behaviors;
using Prism.Regions;
using System.Linq;
using System.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Docking;

namespace SmartPlantApp.Common;

public class ClosePaneAction : TriggerAction<RadDocking>
{
    protected override void Invoke(object parameter)
    {
        var stateChangedArgs = parameter as StateChangeEventArgs;
        if (stateChangedArgs == null)
        {
            return;
        }

        var pane = stateChangedArgs.Panes.FirstOrDefault();
        if (pane == null)
        {
            return;
        }

        IRegion region = RegionManager.GetObservableRegion(stateChangedArgs.Source as DependencyObject).Value;
        if (region == null)
        {
            return;
        }

        region.Remove(pane);//会释放掉对应的viewmodel，导致unload时拿不到datacontext
    }
}
